package com.wei800.turnover.action;

import com.wei800.common.action.abs.AbstractBaseAction;
import com.wei800.common.http.RequestData;
import com.wei800.common.http.ResponseData;
import com.wei800.dto.UserDTO;
import com.wei800.dto.UserinfoDTO;
import com.wei800.service.vlaibao.IUserinfoService;
import com.yootk.jwt.service.IEncryptService;
import com.yootk.jwt.service.ITokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/zzx/userinfo/")
@Slf4j
public class UserinfoAction extends AbstractBaseAction { // 必须要继承父类
    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private ITokenService tokenService;

    @PostMapping("add") // 写路径
    public Object add(@RequestBody RequestData<UserinfoDTO> data) {
        log.info("执行用户增加操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.add(data.getReqData()));
        return responseData;
    }
    @PostMapping("add_batch") // 写路径
    public Object addBatch(@RequestBody RequestData<List<UserinfoDTO>> data) {
        log.info("执行批量用户增加操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.addBatch(data.getReqData()));
        return responseData;
    }
    @PostMapping("edit") // 写路径
    public Object edit(@RequestBody RequestData<UserinfoDTO> data) {
        log.info("执行用户修改操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.edit(data.getReqData()));
        return responseData;
    }
    @PostMapping("remove") // 写路径
    public Object remove(@RequestBody RequestData<List<Long>> data) {
        log.info("执行用户删除操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.remove(data.getReqData().toArray(new Long [] {})));
        return responseData;
    }
    @PostMapping("list")
    public Object list(@RequestBody RequestData<Map<String, Object>> data) {
        log.info("执行用户数据查询操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.list(data.getReqData()));
        return responseData;
    }
    @PostMapping("get")
    public Object get(@RequestBody RequestData<Map<String, Object>> data) {
        log.info("执行用户数据查询操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.get((Long)data.getReqData().get("id")));
        return responseData;
    }
    @PostMapping("get_more")
    public Object getMore(@RequestBody RequestData<List<Long>> data) {
        log.info("执行用户数据查询操作，数据：{}", data);
        ResponseData responseData = new ResponseData();
        responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
        responseData.setRespData(this.userinfoService.get(data.getReqData().toArray(new Long [] {})));
        return responseData;
    }
    @PostMapping("login_using_phone")
    public Object loginByPhone(@RequestBody RequestData<UserinfoDTO> data) {
        log.info("执行用户登录操作，数据：{}", data);
        UserinfoDTO dto = this.userinfoService.loginByPhone(data.getReqData());
        ResponseData responseData = new ResponseData();
        if (dto != null) { // 用户登录成功
            responseData.setRespCode(HttpStatus.OK.value()); // HTTP响应状态
            responseData.setRespMsg("用户登录成功");
            responseData.setRespData(this.tokenService.createToken(String.valueOf(dto.getId()), Map.of("userinfo", dto)));
        } else {
            responseData.setRespCode(HttpStatus.NOT_FOUND.value()); // HTTP响应状态
            responseData.setRespMsg("用户登录失败，错误的用户名或密码");
        }
        return responseData;
    }
}
